<?php
require_once 'config.php';

// 检查用户是否已登录且是管理员
if (!isLoggedIn() || !isAdmin()) {
    $_SESSION['flash_message'] = "您没有权限访问管理后台";
    $_SESSION['flash_type'] = "danger";
    redirect('index.php');
}

// 获取统计数据
try {
    // 图书总数
    $stmt = $pdo->query("SELECT COUNT(*) FROM books");
    $total_books = $stmt->fetchColumn();
    
    // 用户总数
    $stmt = $pdo->query("SELECT COUNT(*) FROM users");
    $total_users = $stmt->fetchColumn();
    
    // 借阅总数
    $stmt = $pdo->query("SELECT COUNT(*) FROM borrowings");
    $total_borrowings = $stmt->fetchColumn();
    
    // 当前借出的图书数
    $stmt = $pdo->query("SELECT COUNT(*) FROM borrowings WHERE status = 'borrowed'");
    $current_borrowings = $stmt->fetchColumn();
    
    // 逾期未还的图书数
    $stmt = $pdo->query("SELECT COUNT(*) FROM borrowings WHERE status = 'borrowed' AND return_date < CURRENT_DATE()");
    $overdue_borrowings = $stmt->fetchColumn();
    
    // 最近10条借阅记录
    $stmt = $pdo->query("SELECT b.*, u.username, bk.title 
                        FROM borrowings b 
                        JOIN users u ON b.user_id = u.user_id 
                        JOIN books bk ON b.book_id = bk.book_id 
                        ORDER BY b.borrow_id DESC LIMIT 10");
    $recent_borrowings = $stmt->fetchAll();
    
    // 最受欢迎的5本图书
    $stmt = $pdo->query("SELECT b.book_id, b.title, b.author, COUNT(br.borrow_id) as borrow_count 
                        FROM books b 
                        JOIN borrowings br ON b.book_id = br.book_id 
                        GROUP BY b.book_id 
                        ORDER BY borrow_count DESC 
                        LIMIT 5");
    $popular_books = $stmt->fetchAll();
    
} catch(PDOException $e) {
    $_SESSION['flash_message'] = "获取统计数据失败: " . $e->getMessage();
    $_SESSION['flash_type'] = "danger";
}

// 页面标题
$page_title = "管理后台 - 图书借阅系统";

// 引入头部
include 'header.php';
?>

<h2 class="mb-4">管理后台</h2>

<div class="row mb-4">
    <div class="col-md-3 mb-4">
        <div class="card bg-primary text-white h-100">
            <div class="card-body text-center">
                <h1 class="display-4"><?php echo $total_books; ?></h1>
                <p class="lead">图书总数</p>
                <a href="admin_books.php" class="btn btn-light btn-sm">管理图书</a>
            </div>
        </div>
    </div>
    <div class="col-md-3 mb-4">
        <div class="card bg-success text-white h-100">
            <div class="card-body text-center">
                <h1 class="display-4"><?php echo $total_users; ?></h1>
                <p class="lead">注册用户</p>
                <a href="admin_users.php" class="btn btn-light btn-sm">管理用户</a>
            </div>
        </div>
    </div>
    <div class="col-md-3 mb-4">
        <div class="card bg-info text-white h-100">
            <div class="card-body text-center">
                <h1 class="display-4"><?php echo $total_borrowings; ?></h1>
                <p class="lead">借阅总数</p>
                <a href="admin_borrowings.php" class="btn btn-light btn-sm">查看借阅</a>
            </div>
        </div>
    </div>
    <div class="col-md-3 mb-4">
        <div class="card <?php echo $overdue_borrowings > 0 ? 'bg-danger' : 'bg-secondary'; ?> text-white h-100">
            <div class="card-body text-center">
                <h1 class="display-4"><?php echo $overdue_borrowings; ?></h1>
                <p class="lead">逾期未还</p>
                <a href="admin_overdue.php" class="btn btn-light btn-sm">处理逾期</a>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-6">
        <div class="card mb-4">
            <div class="card-header">
                <h4>最近借阅记录</h4>
            </div>
            <div class="card-body">
                <?php if (empty($recent_borrowings)): ?>
                    <p class="card-text">暂无借阅记录</p>
                <?php else: ?>
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>用户</th>
                                    <th>图书</th>
                                    <th>借阅日期</th>
                                    <th>状态</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($recent_borrowings as $borrowing): ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars($borrowing['username']); ?></td>
                                        <td><?php echo htmlspecialchars($borrowing['title']); ?></td>
                                        <td><?php echo date('Y-m-d', strtotime($borrowing['borrow_date'])); ?></td>
                                        <td>
                                            <?php if ($borrowing['status'] === 'borrowed'): ?>
                                                <?php 
                                                $return_date = strtotime($borrowing['return_date']);
                                                $today = time();
                                                if ($return_date < $today): 
                                                ?>
                                                    <span class="badge badge-danger">逾期未还</span>
                                                <?php else: ?>
                                                    <span class="badge badge-primary">借阅中</span>
                                                <?php endif; ?>
                                            <?php else: ?>
                                                <span class="badge badge-success">已归还</span>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                    <a href="admin_borrowings.php" class="btn btn-outline-primary btn-sm">查看全部</a>
                <?php endif; ?>
            </div>
        </div>
        
        <div class="card">
            <div class="card-header">
                <h4>快捷操作</h4>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6 mb-2">
                        <a href="admin_add_book.php" class="btn btn-primary btn-block">添加新图书</a>
                    </div>
                    <div class="col-md-6 mb-2">
                        <a href="admin_add_user.php" class="btn btn-success btn-block">添加新用户</a>
                    </div>
                    <div class="col-md-6 mb-2">
                        <a href="admin_books.php" class="btn btn-info btn-block">图书管理</a>
                    </div>
                    <div class="col-md-6 mb-2">
                        <a href="admin_users.php" class="btn btn-secondary btn-block">用户管理</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-6">
        <div class="card mb-4">
            <div class="card-header">
                <h4>最受欢迎图书</h4>
            </div>
            <div class="card-body">
                <?php if (empty($popular_books)): ?>
                    <p class="card-text">暂无数据</p>
                <?php else: ?>
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>图书名称</th>
                                    <th>作者</th>
                                    <th>借阅次数</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($popular_books as $book): ?>
                                    <tr>
                                        <td>
                                            <a href="book_detail.php?id=<?php echo $book['book_id']; ?>">
                                                <?php echo htmlspecialchars($book['title']); ?>
                                            </a>
                                        </td>
                                        <td><?php echo htmlspecialchars($book['author']); ?></td>
                                        <td><span class="badge badge-primary"><?php echo $book['borrow_count']; ?></span></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            </div>
        </div>
        
        <div class="card">
            <div class="card-header">
                <h4>系统状态</h4>
            </div>
            <div class="card-body">
                <p><strong>当前借出图书:</strong> <?php echo $current_borrowings; ?> 本</p>
                <p><strong>可借图书总数:</strong> 
                    <?php 
                    try {
                        $stmt = $pdo->query("SELECT SUM(available) FROM books");
                        echo $stmt->fetchColumn() . " 本";
                    } catch(PDOException $e) {
                        echo "获取失败";
                    }
                    ?>
                </p>
                <p><strong>系统时间:</strong> <?php echo date('Y-m-d H:i:s'); ?></p>
                <p><strong>PHP 版本:</strong> <?php echo phpversion(); ?></p>
                <p><strong>数据库类型:</strong> MySQL</p>
                
                <div class="alert alert-info">
                    <strong>提示:</strong> 您可以在此管理系统的各项功能，包括图书管理、用户管理和借阅管理等。
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?> 